home *** CD-ROM | disk | FTP | other *** search
/ ASME's Mechanical Engine…ing Toolkit 1997 December / ASME's Mechanical Engineering Toolkit 1997 December.iso / c_lang / z150_src.lzh / MSTIME.I < prev    next >
Encoding:
Text File  |  1987-07-12  |  2.8 KB  |  92 lines

  1. #ifndef LINT
  2. static char mstimeid[]="@(#) mstime.i 1.2 87/05/03 16:00:02";
  3. #endif /* LINT */
  4.  
  5. #define BASEYEAR 1970
  6.  
  7. /****************
  8. Function mstime() converts time in seconds since January 1 of BASEYEAR
  9. to MS-DOS format date and time.
  10. */
  11. mstime(longtime, date, time)
  12. long longtime;       /* input:  seconds since Jan 1, BASEYEAR   */
  13. int *date, *time;    /* output: MS-DOS format date and time */
  14.  
  15. {
  16.    static int daysinmo[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  17. #define FEBRUARY 1
  18.    int year, month, day, hour, min, sec;
  19.    long secsinhour, secsinday, secsinyear, secsinleapyear;
  20.  
  21.    int leapyear;                             /* is this a leap year? */
  22.    int done;                                 /* control variable */
  23.  
  24.    secsinhour = (long) (60 * 60);            /* seconds in an hour */
  25.    secsinday  = 24 * secsinhour;             /* seconds in a day */
  26.    secsinyear = 365 * secsinday;             /* seconds in a year */
  27.    secsinleapyear = secsinyear + secsinday;  /* seconds in a leap year */
  28.  
  29. #ifdef DEBUG
  30. printf("mstime:  input longtime = %ld\n", longtime);
  31. #endif
  32.  
  33.    /* We can't handle dates before 1970 so force longtime positive */
  34.    if (longtime < 0)
  35.       longtime = 0;
  36.  
  37.    /* 
  38.    Step through years from BASEYEAR onwards, subtracting number of
  39.    seconds in each, stopping just before longtime would become negative.
  40.    */
  41.    year = BASEYEAR;
  42.    done = 0;
  43.    while (!done) {
  44.       long yearlength;
  45.       leapyear = (year % 4 == 0 && year % 100 != 0 || year % 400 == 0);
  46.       if (leapyear)
  47.          yearlength = secsinleapyear;
  48.       else
  49.          yearlength = secsinyear;
  50.  
  51.       if (longtime >= yearlength) {
  52.          longtime -= yearlength;
  53.          year++;
  54.       } else
  55.          done++;
  56.    }
  57.  
  58.    /* Now `year' contains year and longtime contains remaining seconds */
  59.    daysinmo[FEBRUARY] = leapyear ? 29 : 28;
  60.  
  61.    month = 0; /* range is 0:11 */
  62.    while (longtime > daysinmo[month] * secsinday) {
  63.       longtime = longtime - daysinmo[month] * secsinday;
  64.       month++;
  65.    }
  66.    month++; /* range now 1:12 */
  67.  
  68.    day = longtime / secsinday;     /* day of month, range 0:30 */
  69.    longtime = longtime % secsinday;
  70.    day++;                         /* day of month, range 1:31 */
  71.  
  72.    hour = longtime / secsinhour;       /* hours, range 0:23 */
  73.    longtime = longtime % secsinhour;
  74.  
  75.    min = longtime / 60L;               /* minutes, range 0:59 */
  76.    longtime = longtime % 60L;
  77.  
  78.    sec = longtime;                     /* seconds, range 0:59 */
  79.  
  80. #ifdef DEBUG
  81. printf("mstime:  date = %4d/%02d/%02d   time = %02d:%02d:%02d\n",
  82.       year, month, day, hour, min, sec);
  83. if (leapyear)
  84.    printf("(leap year)\n");
  85. #endif
  86.  
  87.    if (year < 1980)
  88.       year = 1980;
  89.    *date = day + (month << 5) + ((year - 1980) << 9);
  90.    *time = (sec / 2) + (min << 5) + (hour << 11);
  91. }
  92.